package com.linkwechat.web.component;

import com.linkwechat.common.core.domain.entity.WeCorpAccount;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.tenant.TenantContextHolder;
import com.linkwechat.wecom.service.IWeCorpAccountService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WeCorpAccountComponent {
    @Autowired
    private IWeCorpAccountService weCorpAccountService;
    @Autowired
    RedisCache redisCache;

    public WeCorpAccount getWeCorpAccountInfo(){
        String tenantCode=TenantContextHolder.getTenantCode();
        WeCorpAccount weCorpAccount=redisCache.getCacheObject(tenantCode+"_CorpAccount");
        if(ObjectUtils.isEmpty(weCorpAccount)){
            weCorpAccount =weCorpAccountService.findValidWeCorpAccount();
            if(ObjectUtils.isNotEmpty(weCorpAccount)){
                redisCache.setCacheObject(tenantCode+"_CorpAccount",weCorpAccount);
            }
        }
        return weCorpAccount;
    }

}
